/*
 *	This file is part of Lounge-Cast.
 *
 *  Author:  	Markus Moormann
 *	E-mail:  	mmoorman@uni-osnabrueck.de
 *	Copyright:	University of Osnabrueck (Zentrum Virtuos)
 * 	Website:	www.uni-osnabrueck.de
 *	Date:		16.02.2011
 *
 *	Lounge-Cast is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *	Lounge-Cast is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Lounge-Cast.  If not, see <http://www.gnu.org/licenses/>.
 */
package backend.service;

import java.io.File;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;
import java.util.List;

import org.apache.log4j.Logger;

import backend.dbConnection.ConnectionHelper;
import backend.exception.DAOException;
import backend.vo.UploadedFileVO;
import backend.vo.UserVO;

public class UploadedFileService {

	/**
	 * a logger to log errors, debug information, etc.
	 */
	private static Logger LOG = Logger.getLogger(UploadedFileService.class);
	/**
	 * the connection to database
	 */
	private Connection connection;
	/**
	 * the statement so send to database
	 */
	private Statement statement;
	
	/**
	 * get a list of uploaded files from a certain user
	 * @param user the user to get the files from
	 * @return a <code>List&lt;UploadedFileVO&gt;</code>
	 */
	public List<UploadedFileVO> getUploadedFiles(UserVO user) {
		List<UploadedFileVO> result = new LinkedList<UploadedFileVO>();
		
		try {
			connection = ConnectionHelper.getConnection();
			statement = connection.createStatement();
				
			String select = "SELECT " +
								"url," +
								"filename " +
							"FROM " +
								"uploaded_files " +
							"WHERE " +
								"uploaded_by = " + user.user_id;
			ResultSet rs = statement.executeQuery(select);
			while(rs.next())
			{
				result.add(UploadedFileVO.getInstance(rs.getString("url"), rs.getString("filename")));
			}
		} catch (SQLException e) {
			LOG.error(e);
		} finally {
			ConnectionHelper.close(statement);
			
		}
		
		
		return result;
	}
	
	public boolean deleteFile(String file_id) {
		boolean result = false;
		
		try {
			connection = ConnectionHelper.getConnection();
			statement = connection.createStatement();
			
			String getFilePath = 	"SELECT " +
										"path " +
									"FROM " +
										"uploaded_files" +
									"WHERE " +
										"file_id = '" + file_id + "'";
			ResultSet rs = statement.executeQuery(getFilePath);
			if(rs.next())
			{
				if(new File(rs.getString("path")).delete()){
					String delete = "DELETE FROM " +
										"uploaded_files " +
									"WHERE " +
										"file_id = '" + file_id + "'";
					statement.execute(delete);
					result = true;
				}
			}
			
		} catch(SQLException e) {
			LOG.error(e);
			throw new DAOException(e);
		} finally {
			
			ConnectionHelper.close(statement);
		}
		return result;
	}
	
	
}
